package com.leff.cards.ui.fragment;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.leff.cards.R;
import com.leff.cards.data.AppState;
import com.leff.cards.data.Contacts;
import com.leff.cards.ui.util.UserListAdapter;
import com.leff.cards.util.FacebookSession;
import com.leff.cards.util.Settings;
import com.leff.notifications.NotificationCenter;
import com.leff.notifications.NotificationListener;

public class GroupMembersFragment extends SherlockFragment implements NotificationListener {

	private UserListAdapter mAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mAdapter = new UserListAdapter(getActivity());
		this.setHasOptionsMenu(true);

		NotificationCenter.registerListener(this, AppState.GROUPS_REFRESHED);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if(container == null) {
			return null;
		}
		
		ListView list = new ListView(getActivity());
		list.setAdapter(mAdapter);
		
		return list;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		
		inflater.inflate(R.menu.members_menu, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		if(item.getItemId() == R.id.menu_invite_member) {
			showInviteDialog();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		NotificationCenter.unregisterListener(this, AppState.GROUPS_REFRESHED);
	}

	@Override
	public void onNotificationPosted(String action, Object data, Object sender) {
		mAdapter.dataRefreshed();
	}

	private void showInviteDialog() {
		
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		
		builder.setTitle(R.string.invite_user_title);
		
		final Contacts list = Contacts.get();
		builder.setMultiChoiceItems(list.getFriendNames(), list.getSelections(), new OnMultiChoiceClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which, boolean selected) {
				list.setSelection(which, selected);
			}
		});
		
		builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				if(Settings.getPostToFacebook() && Settings.getInvitePostPreference() != Settings.FB_POST_NEVER) {
					
					if(Settings.getInvitePostPreference() == Settings.FB_POST_PROMPT) {
						showFacebookPrompt();
					}
					else {
						ArrayList<Long> ids = list.getSelectedFbIds();
						for(Long id : ids) {
							FacebookSession.getSession().postInvitation(id, AppState.get().getActiveGroup());
						}
						sendInvites();
					}
				}
				else {
					sendInvites();
				}
			}
		});
		builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		
		builder.create().show();
	}
	
	private void sendInvites() {
		
		Contacts list = Contacts.get();
		long groupId = AppState.get().getActiveGroup().id;
		
		list.dispatchInvitations(groupId);
	}
	
	private void showFacebookPrompt() {
		
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		
		builder.setTitle(R.string.facebook_prompt_title);
		builder.setMessage(R.string.facebook_invite_message);
		
		
		
		builder.setPositiveButton(R.string.share, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				ArrayList<Long> ids = Contacts.get().getSelectedFbIds();
				for(Long id : ids) {
					FacebookSession.getSession().postInvitation(id, AppState.get().getActiveGroup());
				}
				sendInvites();
			}
		});
		builder.setNegativeButton(R.string.no_thanks, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				sendInvites();
			}
		});
		
		builder.create().show();
	}
}
